package praktikumid.w10.p2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;

/**
 * An abstract class for all the shapes. 
 * The idea is that every shape knows how
 * to draw itself. Also, every shape
 * knows how to handle different
 * mouse events. DrawPanel just delegates
 * all the events to the shapes.
 * @author Ago
 *
 */
public abstract class Shape {
	/**
	 * Default color for every shape.
	 */
	public static final Color DEFAULT_COLOR = Color.BLACK;
	/**
	 * Highlighted shape color.
	 */
	public static final Color HIGHLIGHT_COLOR = Color.RED;
	
	/**
	 * Color of the shape, let's use default.
	 */
	private Color color = DEFAULT_COLOR;
	/**
	 * Whether shape is finished.
	 * We need this information to know
	 * if we are continuing with this shape.
	 * If the shape is finished, we need to
	 * create a new one.
	 */
	protected boolean isFinished = false;
	
	/**
	 * Whether the shape is highlighted.
	 * We use it to have different color for the shape. 
	 */
	boolean isHighlighted = false;
	
	/**
	 * Getter for isFinished. Instead
	 * of "get", it's common to use "is"
	 * prefix for boolean values.
	 * @return Whether shape is finished
	 */
	public boolean isFinished() {
		return isFinished;
	}

	/**
	 * Defines how the shape is drawn.
	 * Default implementation in abstract class
	 * only sets the color.
	 * @param g Graphics provided by DrawPanel
	 */
	public void paintComponent(Graphics g) {
		if (isHighlighted) {
			// if the shape is highlighted, let's use
			// the specific color
			g.setColor(HIGHLIGHT_COLOR);			
		} else {
			g.setColor(color);
		}
	}

	/**
	 * Returns color of the shape.
	 * @return Color of the shape
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * Sets color of the shape.
	 * @param color Color of the shape
	 */
	public void setColor(Color color) {
		this.color = color;
	}
	
	/**
	 * Highlights the field.
	 */
	public void highlight() {
		isHighlighted = true;
	}
	
	/**
	 * Un-highlights the field (restores the color).
	 */
	public void unHighlight() {
		isHighlighted = false;
	}
	
	/**
	 * Executed on mouse drag event.
	 * (drag - mouse button pressed and mouse moves.)
	 * Delegated from mouse motion listener.
	 * @param e All information available about the event
	 * from the listener
	 */
	public abstract void drag(MouseEvent e);
	/**
	 * Executed on mouse click event.
	 * @param e All information available about the event
	 * from the listener
	 */
	public abstract void click(MouseEvent e);
	/**
	 * Executed on mouse release event.
	 * @param e All information available about the event
	 * from the listener
	 */
	public abstract void release(MouseEvent e);
	
	/**
	 * Returns distance of the shape 
	 * from the certain point.
	 * @param p Point to measure distance from
	 * @return Distance in pixels
	 */
	public abstract int getDistance(Point p);

	/**
	 * Moves the object by specified pixels.
	 * @param dx Horizontal pixels to move the shape.
	 * @param dy Vertical pixels to move the shape.
	 */
	public abstract void move(int dx, int dy);
}
